package cn.dnui_wwt524.dnuifood_16110100524.controller;

import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import cn.dnui_wwt524.dnuifood_16110100524.R;
import cn.dnui_wwt524.dnuifood_16110100524.fragment.CollectFragment;
import cn.dnui_wwt524.dnuifood_16110100524.fragment.HomeFragment;
import cn.dnui_wwt524.dnuifood_16110100524.fragment.MeFragment;
import cn.dnui_wwt524.dnuifood_16110100524.fragment.SearchFragment;

public class MainActivity extends FragmentActivity{
    private ViewPager viewPager;
    private Fragment mTab01,mTab02,mTab03,mTab04;
    private BottomNavigationView bottomNavigationView;
    private List<Fragment> fragmentList;
    private FragmentPagerAdapter fragmentPagerAdapter;
    private BottomNavigationView.OnNavigationItemSelectedListener naviListener;
    private ViewPager.OnPageChangeListener pageChangeListener;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        initView();
        initEvent();
    }

    private void initView() {
        Intent intent = getIntent();
        viewPager = findViewById(R.id.viewPager);

        //获取BottomNavigationView对象
        bottomNavigationView =
                findViewById(R.id.bottomNavigationView);

        //存储5个Fragment的List对象
        fragmentList = new ArrayList<>();

        //创建5个具体的Fragment对象
        HomeFragment fragment1 = new HomeFragment();
        CollectFragment fragment2 = new CollectFragment();
        SearchFragment fragment3 =new SearchFragment();
        MeFragment fragment4 = new MeFragment();


        //添加5个Fragment对象
        fragmentList.add(fragment1);
        fragmentList.add(fragment2);
        fragmentList.add(fragment3);
        fragmentList.add(fragment4);

        //定义ViewPager的适配器
    }
    private void initEvent(){
        fragmentPagerAdapter =
                new FragmentPagerAdapter( getSupportFragmentManager() ) {

                    @Override
                    public Fragment getItem(int position) {

                        //返回fragmentList中与页面索引号对应的Fragment
                        return fragmentList.get(position);
                    }

                    @Override
                    public int getCount() {

                        //返回页面数，一个页面对应一个Fragment对象
                        return fragmentList.size();
                    }
                };

        //为ViewPager设置适配器
        viewPager.setAdapter(fragmentPagerAdapter);

        //定义BottomNavigationView导航监听
        naviListener = new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {

                //获取底部导航的id
                int id = item.getItemId();
                //判断导航id，显示与导航按钮对应的Fragment对象
                switch (id){
                    case R.id.shouye:

                        viewPager.setCurrentItem(0);
                        break;
                    case R.id.collect:
                        viewPager.setCurrentItem(1);
                        break;
                    case R.id.search:
                        viewPager.setCurrentItem(2);
                        break;
                    case R.id.my:
                        viewPager.setCurrentItem(3);
                        break;
                }

                return true;//一定返回true
            }
        };

        //为BottomNavigationView添加监听器对象
        bottomNavigationView.setOnNavigationItemSelectedListener(naviListener);

        //实例化OnPageChangeListener对象
        pageChangeListener = new ViewPager.OnPageChangeListener() {


            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {

                //判断页面索引，选中页面对应的底部导航按钮
                switch (position){

                    case 0:
                        //页面0，"博文"按钮被选中
                        bottomNavigationView.setSelectedItemId(R.id.shouye);
                        break;
                    case 1:
                        bottomNavigationView.setSelectedItemId(R.id.collect);
                        break;
                    case 2:
                        bottomNavigationView.setSelectedItemId(R.id.search);
                        break;
                    case 3:
                        bottomNavigationView.setSelectedItemId(R.id.my);
                        break;
                }

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        };

        //为ViewPager添加监听器对象
        viewPager.addOnPageChangeListener(pageChangeListener);

    }

}
